package core.view.dirlist
{
	import core.event.CopyFileEvent;
	import core.view.CopyFileView;
	
	import flash.events.TextEvent;
	import flash.filesystem.File;
	import flash.text.TextField;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;
	import org.aisy.skin.AisySkin;
	import org.aisy.ulist.UListEvent;
	import org.aisy.ulist.UListLabel;

	internal class DirUListLabel extends UListLabel
	{
		protected var _textField:TextField;
		
		public function DirUListLabel(name:String, data:*)
		{
			super(name, data);
			init();
		}
		
		protected function init():void
		{
			_textField = new TextField();
			_textField.defaultTextFormat = AisySkin.TEXTFORMAT();
			_textField.autoSize = "left";
			_textField.selectable = false;
			_textField.addEventListener(TextEvent.LINK, __linkHandler);
//			__change(iData);
			addChild(_textField);
		}
		
		protected function __change(data:*):void
		{
			if (null === data) return;
			var h:String = '<a href="event:0">~~~</a>\r';
			for each (var i:String in data.keys) {
				switch (i) {
					case "targetList":
					case "sourceList":
						h += i + ":\r" + getHtmlText(data[i]);
						break;
					default:
						h += getHtmlText(data[i]);
						break;
				}
			}
			_textField.wordWrap = false;
			_textField.htmlText = "<b>" + h + "</b>";
			if (_textField.width > Ais.IMain.stage.stageWidth) {
				_textField.wordWrap = true;
				_textField.width = Ais.IMain.stage.stageWidth;
			}
			drawBg(0x7F7F7F);
			TEvent.trigger(CopyFileEvent.COPY_FILE_EVENT, CopyFileEvent._CHANGE, data);
		}
		
		protected function getHtmlText(data:*, indent:String = ""):String
		{
			var str:String = "";
			if (data is Object) {
				for (var i:* in data) {
					str += indent + data[i] + '\r';
				}
			}
			else {
				str = data.toString();
			}
			return str;
		}
		
		protected function drawBg(color:uint, alpha:Number = 1):void
		{
			this.graphics.clear();
			this.graphics.beginFill(color, alpha);
			this.graphics.drawRoundRect(0, 0, this.width, this.height, 20);
			this.graphics.endFill();
		}
		
		protected function __linkHandler(e:TextEvent):void
		{
			switch (e.text) {
				case "0":
					var arr:Array = [];
					var f:File;
					for each (var i:String in iData.sourceList) {
						f = new File(i);
						if (f.exists === true) {
							arr = arr.concat(f.isDirectory === true ? f.getDirectoryListing() : [f]);
						}
						f.cancel();
					}
					CopyFileView.getInstance().copyFiles(arr);
					f = null;
					arr = null;
					break;
			}
		}
		
		override protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case UListEvent.RADIO_SELECT:
					__change(data);
					break;
			}
			super.__triggerHandler(type, data);
		}
		
		override public function clear():void
		{
			_textField.removeEventListener(TextEvent.LINK, __linkHandler);
			_textField = null;
			super.clear();
		}
		
	}
}